package com.yungu.swift.system.marketing.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.marketing.dao.MarketingChannelMapper;
import com.yungu.swift.system.marketing.model.dto.MarketingChannelDto;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftDto;
import com.yungu.swift.system.marketing.model.param.AdminCouponChannelParam;
import com.yungu.swift.system.marketing.service.MarketingChannelService;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

import static com.yungu.swift.system.config.NacosListener.SYS_CONFIG;

/**
 * @Description:
 * @Author cuixiuyin
 * @Date 2018/11/22 15:56
 * @Version 1.0
 */
@Service
public class MarketingChannelServiceImpl extends BaseServiceImpl<MarketingChannelDto> implements MarketingChannelService {

    @Autowired
    private MarketingChannelMapper marketingChannelMapper;
    @Autowired
    private MarketingGiftServiceImpl marketingGiftService;

    @Override
    protected IMapper<MarketingChannelDto> getMapper() {
        return marketingChannelMapper;
    }

    @Override
    public ResponseData<String> findChannelUuidByConditions(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(marketingChannelMapper.findChannelUuidByConditions(params));
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminCouponChannelParam couponChannelParam) {
        ResponseData<MarketingGiftDto> responseData = marketingGiftService.get(couponChannelParam.getGiftUuid());
        if (!responseData.isSuccess() || responseData.getData() == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到礼包信息");
        }
        MarketingGiftDto giftDto = responseData.getData();
        MarketingChannelDto marketingChannelDto = new MarketingChannelDto();
        marketingChannelDto.setName(couponChannelParam.getName());
        // 更新
        if (StringUtils.isNotEmpty(couponChannelParam.getUuid())) {
            marketingChannelDto.setUuid(couponChannelParam.getUuid());
            marketingChannelDto.setUpdateBy(couponChannelParam.getUserUuid());
            marketingChannelDto.setUpdateOn(new Date());
            return edit(marketingChannelDto);
        }
        // 新增
        marketingChannelDto.setUuid(StringUtils.buildUUID());
        marketingChannelDto.setGiftUuid(giftDto.getUuid());
        marketingChannelDto.setPreset(2);
        marketingChannelDto.setAppid(giftDto.getAppid());
        marketingChannelDto.setAgentUuid(giftDto.getAgentUuid());
        marketingChannelDto.setCompanyUuid(giftDto.getCompanyUuid());
        StringBuilder stringBuilder = new StringBuilder();
        // 拼接config.properties中配置的参数
        stringBuilder.append(SYS_CONFIG.getApiHost()).append("/yue/qrCodeView?giftUuid=").append(giftDto.getUuid())
                .append("&appid=").append(giftDto.getAppid())
                .append("&cid=").append(marketingChannelDto.getUuid());
        // 设置领取地址
        marketingChannelDto.setDrawUrl(stringBuilder.toString());
        marketingChannelDto.setCreateBy(couponChannelParam.getUserUuid());
        marketingChannelDto.setCreateOn(new Date());
        return add(marketingChannelDto);
    }

    @Override
    public ResponseData<Boolean> onlyOne(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(marketingChannelMapper.onlyOne(params) > 0);
    }
}
